﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;
using System.Linq;

namespace Bot.Application.IntentQuestions.Service
{
    public class IntentQuestionAppService : BaseAppService, IIntentQuestionAppService
    {
        private IIntentQuestionDomainService _domainService;

        public IntentQuestionAppService(IIntentQuestionDomainService domainService) : base()
        {
            this._domainService = domainService;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.INTENTQUESTION, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.INTENTQUESTION, AuditAction.CREATE)]
        public IntentQuestionDto Add(Guid intentId, IntentQuestionCreateDto dto)
        {
            var intentQuestion = Mapper.Map<IntentQuestion>(dto);
            intentQuestion.IntentId = intentId;
            intentQuestion = _domainService.Create(intentQuestion);

            return Mapper.Map<IntentQuestionDto>(intentQuestion);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.INTENTQUESTION, AuditAction.UPDATE)]
        public IntentQuestionDto Update(Guid id, IntentQuestionUpdateDto dto)
        {
            dto.Id = id;
            IntentQuestion intentQuestion = _domainService.Update(Mapper.Map<IntentQuestionUpdateBo>(dto));
            intentQuestion = _domainService.Get(id, "selectedKeywords");
            return Mapper.Map<IntentQuestionDto>(intentQuestion);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.INTENTQUESTION, AuditAction.UPDATE)]
        public List<IntentQuestionDto> BatchCreate(Guid intentId, List<IntentQuestionCreateDto> dto)
        {
            var intentQuestions = _domainService.BatchCreate(intentId, Mapper.Map<List<IntentQuestion>>(dto));
            return Mapper.Map<List<IntentQuestionDto>>(intentQuestions);
        }

        public IntentQuestionDto Get(Guid id, string include = "")
        {
            IntentQuestion intentQuestion = _domainService.Get(id, "selectedKeywords,"+ include);
            return Mapper.Map<IntentQuestionDto>(intentQuestion);
        }

        public IEnumerable<IntentQuestionDto> GetList(Guid intentId, string include="")
        {
            IEnumerable<IntentQuestion> list = _domainService.GetByIntentId(intentId, "selectedKeywords,"+include);
            return Mapper.Map<IEnumerable<IntentQuestionDto>>(list);
        }
  
    }
}
